package com.game.element
{
	import com.game.manager.LayerManager;
	import com.game.view.scene.DiTuView;
	import com.interfaces.game.GlobalAPI;
	
	import flash.geom.Point;
	import flash.utils.Dictionary;
	import flash.utils.getTimer;

	public class PlayerElement extends BaseElement {
		
		public var baseWeapon:BaseWeapon;
		public var up:Boolean = false;
		public var down:Boolean = false;
		public var left:Boolean = false;
		public var right:Boolean = false;
		
		public var hp:Number = 200;
		public var maxHp:Number = 200;
		
//		     1
//		-2		4
//	-3		 		3
//		-4		2	
//			-1
		private static const dddd:Object = {
			"1":new Point(0,-1),
			"4":new Point(1,-1),
			"3":new Point(1,0),
			"2":new Point(1,1),
			"-1":new Point(0,1),
			"-2":new Point(-1,-1),
			"-3":new Point(-1,0),
			"-4":new Point(-1,1)
		}
		public var speed:Number = 80;
		private var lastCheckTime:Number = 0;
		
		public var startReloading:Number = 0;
		
		public var killedCnt:int = 0;
		
		public var addSpeed:Number = 0;
		
		public function PlayerElement()
		{
			super();
			equipWeapon(new BaseWeapon)
		}
		
		public function equipWeapon(baseWeapon:BaseWeapon):void{
			if(baseWeapon == null){
				return;
			}
			if(this.baseWeapon){
				GlobalAPI.tickManager.unRegeditTick(this.baseWeapon);
			}
			this.baseWeapon = baseWeapon;
			this.baseWeapon.playerElement = this;
			GlobalAPI.tickManager.regeditTick(this.baseWeapon);
		}
		
		public function used(value:Boolean):void{
			if(value == true){
				
			}else{
				
			}
		}
		
		public function fire(targetPoint:Point):void{
			baseWeapon.fire(new Point(this.x,this.y),targetPoint);
		}
		
		override public function exce():void {
			if(lastCheckTime ==0){
				lastCheckTime = getTimer();
				return;
			}
			var d:Number = getTimer()-lastCheckTime ;
			lastCheckTime = getTimer();
			
			var ds :Number = d/1000*(speed+addSpeed);
			var p2:Point = getTarKey();
			if(p2 == null){
				return;
			}
			var p:Point = new Point(this.x,this.y);
			p2.normalize(ds);
			
			var dd:Point = p.add(p2);
			
			var al:int = DiTuView.mask.getPixel32(dd.x/2,dd.y/2)>>6;
			if(al !=0){
				return;
			}
			LayerManager.diTuView.x +=  this.x - dd.x;
			LayerManager.diTuView.y +=  this.y - dd.y;
			
			LayerManager.sceneView.x +=  this.x - dd.x;
			LayerManager.sceneView.y +=  this.y - dd.y;
			
			this.x = dd.x;
			this.y = dd.y;
			
			
			if(startReloading > 0){//正在换子弹
				if(getTimer() > startReloading + baseWeapon.reloadNeedTime){
					startReloading = 0;
					baseWeapon.reloadClipComplete();
					reloadBar.visible = false;
				}else{
					reloadBar.visible = true;
					reloadBar.setValue(getTimer()-startReloading,baseWeapon.reloadNeedTime);
				}
			}
		}
		
		private function getTarKey():Point{
			var de:int = 0;
			if(up) de += 1;
			if(down) de += -1;
			if(left) de += -3;
			if(right) de += 3;
			
			if(de == 0){
				return null;
			}
			return dddd[de+""];
		}
		
		public function reloadClip(reloadNeedTime:Number):void{
			if(baseWeapon.totalBullets == 0){
				equipWeapon(new BaseWeapon);
				return;
			}
			startReloading = getTimer();
		}
		
		public function reduceHp(dam:Number,atk:BaseElement):Number{
			hp -= dam;
			if(hp <0){
				hp =0
			}
			return hp;
		}
	}
}